package com.fbinggun.fattribute.listener;

import com.fbinggun.fattribute.FAttributePlugin;
import com.fbinggun.fattribute.attribute.AAttack;
import com.fbinggun.fattribute.attribute.AOther;
import com.fbinggun.fattribute.attribute.AttributeManage;
import com.fbinggun.fattribute.attribute.IAttribute;
import com.fbinggun.fattribute.attribute.attack.PoisonAttack;
import com.fbinggun.fattribute.attribute.attack.WitherAttack;
import com.fbinggun.fattribute.effect.BuffEffect;
import com.fbinggun.fattribute.effect.IEffect;
import com.fbinggun.fattribute.data.DataManage;
import com.fbinggun.fattribute.data.EntityData;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Projectile;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;

import java.util.HashMap;
import java.util.Map;

public class AttackListener implements Listener {

    @EventHandler(priority = EventPriority.NORMAL)
    public void attacke(EntityDamageByEntityEvent event){
        EntityData entityData=null;
        if(event.getDamager() instanceof LivingEntity) {
             entityData = DataManage.getData((LivingEntity) event.getDamager());
        }
//        if(event.getDamager() instanceof Projectile){
//            Projectile Projectile = (org.bukkit.entity.Projectile) event.getDamager();
//           if( Projectile.getShooter() instanceof Player){
//               entityData = DataManage.getData((LivingEntity) Projectile.getShooter());
//           }
//        }
        if(entityData==null){
            return;
        }
            Map<IAttribute,Double> damageMap = new HashMap<>();
            double damage=0;
            for(IAttribute attribute: AttributeManage.getAttributeMap().values()){
                if(attribute instanceof AAttack){
                    AAttack aAttack = (AAttack) attribute;
                    if(entityData.getValue(aAttack.getKey())!=0) {
                        damageMap.put(aAttack, aAttack.getDamage(entityData,event));
                        if(event.getEntity() instanceof LivingEntity){
                            if(event.getCause() == EntityDamageEvent.DamageCause.ENTITY_ATTACK) {
                                aAttack.getEffect(entityData, (LivingEntity) event.getEntity());
                            }
                        }
                    }
                }
            }
            for(IAttribute attribute: AttributeManage.getAttributeMap().values()){
                if(attribute instanceof AOther){
                    AOther other = (AOther) attribute;
                    if(entityData.getValue(other.getKey())!=0) {
                        damageMap = other.modifyDamage(entityData, damageMap, event);
                    }
                }
            }



            for(IEffect effect: entityData.getiEffects().keySet()){
                damageMap = effect.modifyDamage(entityData, damageMap, event);
            }
            if(damageMap.size()!=0){
                for(IAttribute attribute:damageMap.keySet()){
                    damage=damageMap.get(attribute)+damage;
                }
            }

            event.setDamage(event.getDamage()+damage);


    }
    @EventHandler(priority = EventPriority.NORMAL)
    public void attacke(EntityDamageEvent event){
      if(event.getEntity() instanceof LivingEntity){
       EntityData   entityData = DataManage.getData((LivingEntity) event.getEntity());
        for(IEffect effect: entityData.getiEffects().keySet()) {
          if(effect instanceof BuffEffect){
              BuffEffect buffEffect = (BuffEffect) effect;
              if(buffEffect.getBuffAttack() instanceof PoisonAttack) {
                  if (event.getCause() == EntityDamageEvent.DamageCause.POISON) {
                      double damage =buffEffect.getModification();
                      damage=damage-entityData.getValue(FAttributePlugin.plugin.getConfigKey("PoisonDefense"));
                        event.setDamage(event.getDamage()+damage);
                  }
              }
              if(buffEffect.getBuffAttack() instanceof WitherAttack) {
                  if (event.getCause() == EntityDamageEvent.DamageCause.WITHER) {
                      double damage =buffEffect.getModification();
                      damage=damage-entityData.getValue(FAttributePlugin.plugin.getConfigKey("WitherDefense"));
                      event.setDamage(event.getDamage()+buffEffect.getModification());
                  }
              }
          }

        }
        }
    }



}
